/*
 * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
 */
package com.wlh.animation.ipctest;

import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * @author weilh
 *         <b>DATE</b> 2016/8/23.
 */
public class AIDLService extends Service{
    RemoteCallbackList<IBookListener> mCallbacks = new RemoteCallbackList<>();
    private IBinder mBinder = new IBookManager.Stub() {
        @Override
        public List<Book> getBookList() throws RemoteException {
            Log.i("wlh", "getBookList");
            return null;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            Log.i("wlh", "addBook : " + book.name);
            int N = mCallbacks.beginBroadcast();
            book.name += " : server";
            for (int i = 0; i < N; i++ ) {
                mCallbacks.getBroadcastItem(i).onBookAdd(book);
            }
            mCallbacks.finishBroadcast();

        }

        @Override
        public void registerListener(IBookListener listener) throws RemoteException {
            mCallbacks.register(listener);
        }

        @Override
        public void unRegisterListener(IBookListener listener) throws RemoteException {
            mCallbacks.unregister(listener);
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}
